#include "bullet_2.h"

extern QPoint shuttle_pos;

BULLET_2::BULLET_2(bool razlet_v_levo, int angle_attacks, bool stvol_left, QPoint my_coords, SCENE *scene, QTimeLine *global_tl, CASH *cash):QGraphicsObject()
{

    shot = new SOUND_POWER(cash,"Res/Sound/Enemy/Enemy_6/shot.ogg",this);
    shot->Play_Now();

    this->cash = cash;

    switch(angle_attacks){

        case -90:   if(stvol_left) begin = QPoint(my_coords.x()+70,my_coords.y()+36);
                    else begin = QPoint(my_coords.x()+70,my_coords.y()+140);
                    break;

        case -45:   if(stvol_left) begin = QPoint(my_coords.x()+106,my_coords.y()+47);
                    else begin = QPoint(my_coords.x()+33,my_coords.y()+122);
                    break;

        case 0:     if(stvol_left) begin = QPoint(my_coords.x()+20,my_coords.y()+80);
                    else begin = QPoint(my_coords.x()+130,my_coords.y()+80);
                    break;

        case 45:    if(stvol_left) begin = QPoint(my_coords.x()+43,my_coords.y()+45);
                    else begin = QPoint(my_coords.x()+114,my_coords.y()+122);
                    break;

        case 90:    if(stvol_left) begin = QPoint(my_coords.x()+85,my_coords.y()+35);
                    else begin = QPoint(my_coords.x()+85,my_coords.y()+140);
                    break;

        default:    qDebug()<<"switch: Ugol Ataki Ne Raspoznan!";

    }

    point=5;

    border = new QPainterPath();
    im = cash->cash_pixmap->value("Res/weapons/Bullet_2/Bullet_2.png");

    this->setZValue(0.5);

    this->setPos(begin);

    anim_rocket = new QGraphicsItemAnimation();

    rocket_time_line = new QTimeLine(1000);
    rocket_time_line->setEasingCurve(QEasingCurve::Type(1));
    rocket_time_line->setFrameRange(0,1);

    anim_rocket->setTimeLine(rocket_time_line);
    anim_rocket->setItem(this);

    anim_rocket->setPosAt(0,begin);
    anim_rocket->setScaleAt(0,0.5,0.5);

    if(razlet_v_levo)
        anim_rocket->setPosAt(1,QPoint(shuttle_pos.x()+5,shuttle_pos.y()+5) );
    else
        anim_rocket->setPosAt(1,QPoint(shuttle_pos.x()-5,shuttle_pos.y()-5) );

    anim_rocket->setScaleAt(1,1,1);

    rocket_time_line->start();

    collisia_control = new COLLISIA_CONTROL(scene,((QGraphicsObject*)this),this,global_tl,cash,10);

    connect(collisia_control,SIGNAL(Collisia(QPointF,int)),this,SLOT(Damage_Detected(QPointF,int)));
    connect(rocket_time_line,SIGNAL(finished()),SLOT(deleteLater()));

    QTimer::singleShot(500, this, SLOT(set_shape()));
}

QPainterPath BULLET_2::shape() const
{

    return *border;

}

QRectF BULLET_2::boundingRect() const
{

    return QRectF( QPointF(0,0),im->size() );

}

void BULLET_2::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{

    painter->save();
    painter->drawPixmap( 0,0,*im );
    painter->restore();

}

BULLET_2::~BULLET_2()
{
    shot->Stop();
}

int BULLET_2::type() const
{

    return point;

}

void BULLET_2::set_shape()
{
    this->border = cash->cash_poly->value("Res/weapons/Bullet_2/Bullet_2.poly");
}

void BULLET_2::Damage_Detected(QPointF a, int d)
{

    a=QPointF();

    point-=d;

    if(point<=0)
        if(this->scene()){
            disconnect(rocket_time_line,SIGNAL(finished()),this,SLOT(deleteLater()));
            shot->Stop();
            emit Kill_Me_Please((QGraphicsObject*)this);
        }

}









